package com.cqc.jdk8.Jtext;

import org.junit.Test;

import java.text.ChoiceFormat;
import java.text.MessageFormat;
import java.util.Date;

public class MessageFormatTest {

    /**
     * 日期可以分为  {0,time} {0,date}
     * 数字可以这么写：{0,number,integer}
     * 格式样式语法：{index, type, style}，例如 {1, number, percent}。
     */
    @Test
    public void f1() {
        //源码demo
        int planet = 7;
        String event = "a disturbance in the Force";
        String result = MessageFormat.format("At {1,time} on {1,date}, there was {2} on planet {0,number,integer}.", planet, new Date(), event);
        System.out.println(result);

        int day = 4;
        String pattern = "时间：{0,time}，日期：{0,date}，今天是星期{1,number,integer}，你好：{2}";
        String name = "刘亦菲";
        String result2 = MessageFormat.format(pattern, new Date(), day, name);
        System.out.println(result2);

    }

    @Test
    public void f2() {
        //时间：25-3-13 下午5:06
        String pattern = "时间：{0}";
        String result = MessageFormat.format(pattern, new Date());
        System.out.println(result);
    }

    /**
     * 参数也可以是数组
     */
    @Test
    public void f3() {
        String pattern = "目录{0}下，有{1}个文件";
        Object[] parameters = {"c盘", 100};
        String result = MessageFormat.format(pattern, parameters);
        System.out.println(result);
        //或者
        MessageFormat messageFormat = new MessageFormat(pattern);
        System.out.println(messageFormat.format(parameters));
    }

    @Test
    public void f4() {
        MessageFormat form = new MessageFormat("The disk \"{1}\" contains {0}.");
        double[] filelimits = {0, 1, 2};
        String[] filepart = {"no files", "one file", "{0,number} files"};

        ChoiceFormat fileform = new ChoiceFormat(filelimits, filepart);
        form.setFormatByArgumentIndex(0, fileform);
        int fileCount = 0;//映射filelimits,filelimits又映射filepart。
        String diskName = "MyDisk";
        Object[] testArgs = {new Long(fileCount), diskName};
        System.out.println(form.format(testArgs));
    }

    @Test
    public void f5() {

    }
}
